/**
 * this file will be loaded before server started
 * you can define global functions used in controllers, models, templates
 */

/**
 * use global.xxx to define global functions
 * 
 * global.fn1 = function(){
 *     
 * }
 */

 // console.log(think.isMaster);

const cluster = require('cluster');

const os      = require('os');

const getIp   = function(){

    let devs  = os.networkInterfaces();

    for (let key in devs) {

        let dev = devs[key];

        for (let i = 0; i < dev.length; i++){
            if (dev[i].family == 'IPv4') {
                if(dev[i].address != '127.0.0.1'){
                    return dev[i].address;
                }     
            }
        }
    }
    
    return '127.0.0.1';
}

if (cluster.isMaster) {
    think.config("serverName", think.config("serverName") +'-'+ getIp());
}

if (cluster.isWorker) {
    think.config("serverName", think.config("serverName") +'-'+ getIp() +'-'+ cluster.worker.id);
}